package com.cheerful.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.cheerful.blog.entity.Article;
import com.cheerful.blog.service.ArticleService;
import com.cheerful.blog.to.ArchiveVo;
import com.cheerful.blog.to.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import static com.cheerful.blog.config.constant.CacheEnum.VIEW_NUMS_PREFIX;

/**
 * @author: Wang Zhifu
 * @create: 2022/2/7 20:37
 * @Description:
 */
@Controller
public class CommonController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    @Qualifier("redisTemplate")
    RedisTemplate<String,Object> redisTemplate;

    @GetMapping("/link.html")
    public String link(){
        return "link";
    }

    @GetMapping({"/","/index.html","index"})
    public String index(Model model){
        //查询热门文章3篇
        List<Article> articles=articleService.getHotArticles();
        model.addAttribute("hots",articles);
        return "index";
    }

    @GetMapping("/article/{id}.html")
    public String getArticleDetail(@PathVariable Long id, Model model){
        String viewKey=VIEW_NUMS_PREFIX+id;
        Integer s = (Integer) redisTemplate.opsForValue().get(viewKey);
        if (ObjectUtils.isEmpty(s)){
            redisTemplate.opsForValue().set(viewKey, 1);
        }else{
            redisTemplate.opsForValue().increment(viewKey);
        }
        ArticleVo article = articleService.getArticleDetail(id);
        model.addAttribute("article",article);
        return "article";
    }

    @GetMapping("/getArticleByCategory/{categoryId}")
    public String getArticleByCategory(@PathVariable Long categoryId,Model model){
        List<Article> articles=articleService.getArticleByCategory(categoryId);
        model.addAttribute("articles",articles);
        return "category";
    }

    @GetMapping("category.html")
    public String category(){
        return "category";
    }

    @GetMapping("/archive.html")
    public String archive(Model model){
        List<ArchiveVo> articlesGroupByYearMonth = articleService.getArticlesGroupByYearMonth();
        model.addAttribute("archives",articlesGroupByYearMonth);
        return "archive";
    }

//    @GetMapping("/{path}.html")
//    public String toHtml(@PathVariable String path){
//        return path;
//    }

    @GetMapping("/searchArticle/{content}")
    public String searchArticle(@PathVariable String content,Model model){
        List<Article> articles = articleService.list(new QueryWrapper<Article>().like("title", content));
        model.addAttribute("articles",articles);
        return "search";
    }
}
